package com.supermap.test;

import com.supermap.data.*;
import com.supermap.data.processing.*;
import com.supermap.mapping.Map;

import java.util.HashMap;

public class BatchGenSci {
    public static void main(String[] args) {

        Workspace workspace = new Workspace();
        WorkspaceConnectionInfo conInfo = new WorkspaceConnectionInfo(
                "E:\\MapData\\202411\\sci\\Workspace.smwu");
        conInfo.setType(WorkspaceType.SMWU);
        workspace.open(conInfo);


        Datasource d = workspace.getDatasources().get(0);
        DatasetVector datasetVector = (DatasetVector)d.getDatasets().get("allsci");
        String mapXml = workspace.getMaps().getMapXML("memoryMap");

        DatasourceConnectionInfo connectionInfo = new DatasourceConnectionInfo();
        connectionInfo.setEngineType(EngineType.MEMORY);
        connectionInfo.setAlias("MemoryDatasource");

        Datasource m_datasource = workspace.getDatasources().create(connectionInfo);




        Recordset recordset = datasetVector.getRecordset(false, CursorType.STATIC);


        int num =0;
        while (!recordset.isEOF()) {
            String newName = m_datasource.getDatasets().getAvailableDatasetName("sci_"+num);
            DatasetVectorInfo datasetVectorInfo = new DatasetVectorInfo(newName, DatasetType.REGION);
            DatasetVector newDataset = m_datasource.getDatasets().create(datasetVectorInfo);
            Geometry geometry = recordset.getGeometry();
            java.util.Map map = new HashMap();

            FieldInfos fieldInfos = recordset.getFieldInfos();

//            for (int i = 0; i < fieldInfos.getCount(); i++) {
//                FieldInfo fieldInfo = fieldInfos.get(i);
//                if (!fieldInfo.getName().equalsIgnoreCase("SMID"))
//                    map.put(fieldInfo.getName(), recordset.getFieldValue(fieldInfo.getName()));
//            }

            Recordset newRecordSet = newDataset.getRecordset(true, CursorType.DYNAMIC);
            newRecordSet.addNew(geometry, map);

            newRecordSet.update();

            // 关闭记录集，释放几何对象、记录集
            newRecordSet.close();
            newDataset.computeBounds();

            String newMapXml = mapXml.replaceAll("allsci", newName);

            Map scimap = new Map(workspace);
            scimap.fromXML(newMapXml);
            scimap.setCenter(newDataset.getBounds().getCenter());
            workspace.getMaps().add(newName, scimap.toXML());

            MapCacheBuilder mapCacheBuilder = new MapCacheBuilder();

            mapCacheBuilder.setOutputFolder("E:\\MapData\\202411\\sci\\sciCaches");
            mapCacheBuilder.setMap(scimap);
            mapCacheBuilder.setCacheName(newName);
            mapCacheBuilder.setStorageType(StorageType.Compact);
            mapCacheBuilder.setMultiProcessing(false);
            mapCacheBuilder.setOutputScales(new double[]{1/100000d, 1/50000d,1/20000d});
            mapCacheBuilder.setBounds(newDataset.getBounds());
            mapCacheBuilder.setDPI(96d);
            mapCacheBuilder.setTileFormat(TileFormat.WEBP);
            mapCacheBuilder.setTileSize(TileSize.SIZE256);
            mapCacheBuilder.setTransparent(true);
            mapCacheBuilder.setVersion(MapCacheVersion.VERSION_30);
            mapCacheBuilder.addSteppedListener(steppedEvent -> {
                System.out.println(steppedEvent.getMessage());
//                System.out.println(steppedEvent.getPercent());
            });
            mapCacheBuilder.build(true);




            geometry.dispose();
            newRecordSet.dispose();
            newDataset.dispose();

            recordset.moveNext();

            num++;
        }

        //workspace.save();
        //workspace.dispose();



    }
}
